#include "RandomSampleConsensusModel.h"

#include "core/AppDirectory.h"

namespace {
const QString UserFilename = "pcl.random_sample_consensus_model.json";
}

RandomSampleConsensusModel::RandomSampleConsensusModel()
    : BasePclModel()
{
    setPclHelpPage("https://pcl.readthedocs.io/projects/tutorials/en/master/random_sample_consensus.html");
}

RandomSampleConsensusModel& RandomSampleConsensusModel::get()
{
    static RandomSampleConsensusModel instance;
    if (instance.filename().isEmpty()) {
        instance.setFilename(AppDirectory::get().getAppDocFile(UserFilename));
        instance.load();
    }
    return instance;
}

void RandomSampleConsensusModel::fromJson(const QJsonObject& json, QStringList& errorList)
{
    BasePclModel::fromJson(json, errorList);

    pcdFile_ = json["pcdFile"].toString(pcdFile_);
    sampleSize_ = json["sampleSize"].toInt(sampleSize_);
    sampleMinPos_ = json["sampleMinPos"].toDouble(sampleMinPos_);
    sampleMaxPos_ = json["sampleMaxPos"].toDouble(sampleMaxPos_);
    cloudType_ = json["cloudType"].toInt(cloudType_);
    distanceThreshold_ = json["distanceThreshold"].toDouble(distanceThreshold_);
}

QJsonObject RandomSampleConsensusModel::toJson() const
{
    QJsonObject json = BasePclModel::toJson();

    json.insert("pcdFile", QJsonValue(pcdFile_));
    json.insert("sampleSize", QJsonValue(sampleSize_));
    json.insert("sampleMinPos", QJsonValue(sampleMinPos_));
    json.insert("sampleMaxPos", QJsonValue(sampleMaxPos_));
    json.insert("cloudType", QJsonValue(cloudType_));
    json.insert("distanceThreshold", QJsonValue(distanceThreshold_));

    return json;
}
